
import backgroulds from "./backgrould";
import allVariableJs from "./allVariable";
cc.Class({
    extends: cc.Component,

    properties: {
        timesprite:cc.Node,
        timeArray:[cc.SpriteFrame],
        timeCount:cc.Integer,
        countDownArray:[cc.Node],
        endGameView:cc.Node,
        noendGameView:cc.Node,
        showHubProjess:cc.Node,
        backGroundJs:backgroulds,
        timeBtn:cc.Node,
        SvprogressPrefab: cc.Prefab,
        endCutDown:cc.Node,
        endfinshTimes:cc.Node,
        timePropNum:cc.Integer,//时间道具数量
        cutTimeView:cc.Node,
        timeSound:{
            default: null,
            type: cc.AudioClip
        },
        unitTime:cc.Node,
        decadeTime:cc.Node,
        myTimes:0
        
        
    },

    onLoad () {
   
    
     
    },


    addTimePropNumber:function(){

        if(this.timeCount>allVariableJs.maxSecond-allVariableJs.addTimouse){

            if(cc.isValid(this.showHubProjess)){
                this.showHubProjess.destroy();
              }
              this.showHubProjess = cc.instantiate(this.SvprogressPrefab);
              this.backNode=cc.find("Canvas/backgroundtop");
              this.backNode.addChild(this.showHubProjess);
              this.showHubProjess.getComponent('SVProgressView').init("时间达到最大了")
        
               let finish = cc.callFunc(() => { 
              
                this.showHubProjess.destroy();
            
              }, this);
            let action = cc.sequence(cc.fadeIn(0.6), cc.delayTime(0.5), cc.fadeOut(1), finish);
            this.showHubProjess.runAction(action);
            
            return

        }
    
        this.timeCount=this.timeCount+allVariableJs.addTimouse+1;
    
        this.unschedule(this.countDownL)

        var interval = 1;
        // 重复次数
        var repeat =this.timeCount;
        // 开始延时
        var delay = 0;
        
        this.schedule(this.countDownL, interval, repeat, delay);


    },


  
    addTimeCount:function(){


        var changebig2 = cc.scaleTo(0.2,1.1);

        var finish = cc.callFunc(function(){
    
          var changesmail2 = cc.scaleTo(0.1,1.0);
          this.timeBtn.runAction(changesmail2)
    
       }, this);
    
       var seq = cc.sequence(changebig2,finish)
    
        this.timeBtn.runAction(seq)

        
        if(this.timeCount>88){

            if(cc.isValid(this.showHubProjess)){
                this.showHubProjess.destroy();
              }
              this.showHubProjess = cc.instantiate(this.SvprogressPrefab);
              this.backNode=cc.find("Canvas/backgroundtop");
              this.backNode.addChild(this.showHubProjess);
              this.showHubProjess.getComponent('SVProgressView').init("时间达到最大了")
        
               let finish = cc.callFunc(() => { 
              
                this.showHubProjess.destroy();
            
              }, this);
            let action = cc.sequence(cc.fadeIn(0.6), cc.delayTime(0.5), cc.fadeOut(1), finish);
            this.showHubProjess.runAction(action);
            
            return

        }
    


        if(this.timePropNum>0){


            this.timePropNum=this.timePropNum-1;

            allVariableJs.timeCount=this.timePropNum
    
            this.timeBtn.getChildByName("timePropShow").getChildByName("timePropLabel").getComponent("NumberView").setText(this.timePropNum)

            this.timeCount=this.timeCount+10;
    
            this.unschedule(this.countDownL)
    
    
            var interval = 1;
            // 重复次数
            var repeat =this.timeCount;
            // 开始延时
            var delay = 0;
            
            this.schedule(this.countDownL, interval, repeat, delay);

        }else{

            console.log("道具数量不够");

            if(cc.isValid(this.showHubProjess)){
                this.showHubProjess.destroy();
              }
              this.showHubProjess = cc.instantiate(this.SvprogressPrefab);
              this.backNode=cc.find("Canvas/backgroundtop");
              this.backNode.addChild(this.showHubProjess);
              this.showHubProjess.getComponent('SVProgressView').init("时间道具数量不足")
        
               let finish = cc.callFunc(() => { 
              
                this.showHubProjess.destroy();
            
              }, this);
            let action = cc.sequence(cc.fadeIn(0.6), cc.delayTime(0.5), cc.fadeOut(1), finish);
            this.showHubProjess.runAction(action);
            
        }

    },

    
    setTimeLoad:function(timeNumber){
              
        var  timeArray =timeNumber.split('')
 
        for (let i = 0; i < timeArray.length; i++)
        {
 
        var item = cc.instantiate(this.timesprite);
        
        item.getComponent(cc.Sprite).spriteFrame=this.timeArray[timeArray[i]]

        this.node.addChild(item)
        
        this.countDownArray.push(item)


      }


},


    stopTime:function(){

        this.unschedule(this.countDownL)

    },

     myTimeRun:function(){

        if(this.timeCount==0){
            console.log("取消自己定时器") 
            return
        }

        if(this.myTimes==20){
            console.log("第二等级")
            this.backGroundJs.gradeSecond()

        }else if(this.myTimes==30){
            console.log("第三等级")

            this.backGroundJs.gradeThree()

        }else if(this.myTimes==40){
            console.log("第四等级")
            this.backGroundJs. gradeFour()

        }else{
        //  console.log("第五等级")
        }
        allVariableJs.myPlayTimes=this.myTimes;
        console.log("当前自己玩的时间耗时",this.myTimes)
        this.myTimes=this.myTimes+1

     },

    countDownL:function(){

        if(this.timeCount==0){
            console.log("时间已经为0") 
            this.unschedule(this.myTimeRun)
         cc.audioEngine.stop(this.current);
        allVariableJs.isEnterEndTimes=false

        this.cutTimeView.active=false
        this. endfinshTimes.active=false
         
         if(allVariableJs.wawaCoinAddNumber==0){
             
            this.endGameView.active=true
     
         }else{
            this.endGameView.active=true
         }

         this.unschedule(this.countDownL)
         this.backGroundJs.endGame();
            return

        }

        if(this.timeCount==1){

            this.endCutDown.active=false
            this. endfinshTimes.active=true
          
            var changebig = cc.scaleTo(0.1,1.2);
            var changesmall = cc.scaleTo(0.07,1);
            var seq = cc.sequence(changebig,changesmall);
            this. endfinshTimes.runAction(seq);
            

        }else{
            this. endfinshTimes.active=false
        }
     
        this.timeCount=this.timeCount-1

        if(this.timeCount<=10&&this.timeCount>=1){

            this.current = cc.audioEngine.play(this.timeSound, false, 1);

            allVariableJs.isEnterEndTimes=true

            var that =this
            cc.loader.load({url: "https://h5.taoqi001.com/upload/201901/76a34a4266938997b654703e8ea560d683a432e0.png", type: 'png'}, function (err, img) {
      
                var mylogo  = new cc.SpriteFrame(img); 
      
                that.cutTimeView.getComponent(cc.Sprite).spriteFrame = mylogo;
            
            });
       
            
            this.cutTimeView.active=true
          
            var opaction = cc.fadeTo(0.25, 100);

            var opaction2 = cc.fadeTo(0.25, 200);

            var opaction3 = cc.fadeTo(0.25, 100);

            var opaction4 = cc.fadeTo(0.25, 200);

            var seq = cc.sequence(opaction,opaction2,opaction3,opaction4);
          
            this.cutTimeView.runAction(seq);

            this.endCutDown.active=true

            this.endCutDown.getChildByName("endTimeShow").getComponent("NumberView").setText(this.timeCount)

            var changebig = cc.scaleTo(0.1,1.2);
            var changesmall = cc.scaleTo(0.07,1);
            var seq = cc.sequence(changebig,changesmall);

            this.endCutDown.getChildByName("endTimeShow").runAction(seq);



        }else{

            this.cutTimeView.active=false;
            this.endCutDown.active=false
            allVariableJs.isEnterEndTimes=false
            
        }
  


        var count=this.timeCount

        allVariableJs.timeNumber=this.timeCount

        var  timeArray =String(count).split('')

         var array = new Array()

        for(let i = 0;i<timeArray.length; i++){

             array.push(this.countDownArray[i])
            
        }

        for(let j = 0;j<this.countDownArray.length; j++){

            if(j>=array.length){
                this.countDownArray[j].active=false

            }else{
                for(let i = 0;i<array.length; i++){
                    this.countDownArray[j].active=true
                    array[i].getComponent(cc.Sprite).spriteFrame=this.timeArray[timeArray[i]]
                     
               }
            }
        }
    },

    startTime:function(){
 
        var interval = 1;
        // 重复次数
        var repeat =this.timeCount;
        // 开始延时
        var delay = 0;

        this.schedule(this.countDownL, interval, repeat, delay);

    },

    start () {

    },

    init(gameTimes){

       this.timePropNum=allVariableJs.timeCount
      
       this.unitTime.active=false
       this.decadeTime.active=false
   
       console.log("传过来的数字",gameTimes)
        this.timeCount=gameTimes
  
        this.setTimeLoad(String(this.timeCount))
  
          var interval = 1;
          // 重复次数
          var repeat =this.timeCount;
          // 开始延时
          var delay = 0;
         
          this.schedule(this.countDownL, interval, repeat, delay);


          this.unschedule(this.myTimeRun)

          var myinterval=1;

          var myrepet=10000000;

          this.myTimes=0;

          var mydelay=0;

          this.schedule(this.myTimeRun, myinterval, myrepet, mydelay);


    }

});
